package br.com.moxirpt.view.resource;

import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

import br.com.moxirpt.model.vo.Persistivel;
import br.com.moxirpt.model.vo.PersistivelPrincipal;

public class ResourceFactory {
	
	public static final String RESOURCES_PATH = "br.com.moxirpt.view.resource"; 
	
	public static ResourceBundle[] getResources(PersistivelPrincipal persistivelPrincipal){

		List<ResourceBundle> resources = new ArrayList<ResourceBundle>();
				
		String packagePersistivel = persistivelPrincipal.getClass().getPackage().getName();
		String[] packs = packagePersistivel.split("\\.");
		packagePersistivel = packs[packs.length-1];
			
		String resourceName = null;
		
		for(Persistivel p : persistivelPrincipal.obterObjetosFilho()){
			
			resourceName = p.getClass().getSimpleName();
			resourceName = RESOURCES_PATH + "." + packagePersistivel + "." + resourceName.substring(0,resourceName.length()-2);
			
			resources.add( ResourceBundle.getBundle( resourceName ) );	
			
			resourceName = null;
			
		}
			
		return resources.toArray( new ResourceBundle[resources.size()] ); 
		
	}

	public static ResourceBundle getResource(Class<?> classe){
					
		String packagePersistivel = classe.getPackage().getName();
		String[] packs = packagePersistivel.split("\\.");
		packagePersistivel = packs[packs.length-1];
			
		String resourceName = null;
		
		
		resourceName = classe.getSimpleName();
		resourceName = RESOURCES_PATH + "." + packagePersistivel + "." + resourceName.substring(0,resourceName.length()-2);
			
		return ResourceBundle.getBundle( resourceName ); 
		
	}
}
